import axios from 'axios'

import { Toast } from 'vant'

const instanceaxios = axios.create({
  baseURL: 'https://m.maizuo.com',
  timeout: 5000,
  headers: {
    'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.0.4","e":"15984753841366499679797249","bc":"310100"}'
  }
})

instanceaxios.interceptors.request.use(
  config => {
    // Do something before request is sent
    Toast.loading({
      forbidClick: true,
      className: 'mainToast'
    })
    return config
  },
  error => {
    // Do something with request error
    return Promise.reject(error)
  }
)

instanceaxios.interceptors.response.use(
  response => {
    // Do something before response is sent
    Toast.clear()
    return response.data
  },
  error => {
    // Do something with response error
    return Promise.reject(error)
  }
)

function request(config) {
  const instanceaxios1 = axios.create({
    baseURL: 'http://82.156.11.62:8888/api/private/v1',
    timeout: 5000
  })
  instanceaxios1.interceptors.request.use(
    config => {
      // Do something before request is sent
      return config
    },
    error => {
      // Do something with request error
      return Promise.reject(error)
    }
  )

  instanceaxios1.interceptors.response.use(
    response => {
      // Do something before response is sent
      return response.data
    },
    error => {
      // Do something with response error
      return Promise.reject(error)
    }
  )
  return instanceaxios1(config)
}

export { instanceaxios, request }
